package com.itheima.servise.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.servise.EmpLogService;
import com.itheima.servise.EmpServise;
import com.itheima.utils.JwtUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiseImpl implements EmpServise {
    private static final Logger log = LoggerFactory.getLogger(EmpServiseImpl.class);
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
    //分页查询
    @Override
    public PageBean page(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(), param.getPageSize());
        List<Emp> rows = empMapper.list(param);
        Page<Emp> p = (Page<Emp>) rows;
        return new PageBean(p.getTotal(), p.getResult());
    }
    //新增员工 员工经历
    @Log
    @Transactional
    @Override
    public void add(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            log.info("储存前新增员工信息"+emp);
            empMapper.add(emp);
            log.info("储存后新增员工信息"+emp);


            List<EmpExpr> exprList = emp.getExprList();
            if(exprList != null && exprList.size() > 0){
                for (EmpExpr empExpr : exprList) {
                    empExpr.setEmpId(emp.getId());
                }
                empExprMapper.insertBatch(exprList);
            }
        }finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("新增员工信息"+emp);
            empLogService.insertLog(empLog);
        }
    }

    //查询所有员工
    @Override
    public List<Emp> getAll() {
        return empMapper.list(null);
    }

    //删除员工 员工经历
    @Log
    @Override
    @Transactional
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);

        empExprMapper.deleteEmpId(ids);
    }

    //根据id查询员工
    @Override
    @Transactional
    public Emp getById(Integer id) {
        Emp emp = empMapper.selectEmp(id);
        List<EmpExpr> empExprs = empExprMapper.selectEmpExpr(id);
        emp.setExprList(empExprs);
        return emp;

    }

    //修改员工
    @Log
    @Override
    @Transactional
    public void putEmp(Emp emp) {
        //根据id修改员工基本信息
        empMapper.updateById(emp);
        //根据id删除员工工作经历
        Integer EmpId = emp.getId();
        empExprMapper.deleteEmpId(Arrays.asList(EmpId));
        //根据id新增员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(exprList != null && exprList.size() > 0){
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(emp.getId());
            }
            empExprMapper.insertBatch(exprList);
        }

    }

    //登录
    @Override
    public LoginInfo login(Emp emp) {
        Emp usernameAndPassword = empMapper.getUsernameAndPassword(emp);
        if (usernameAndPassword != null){
            //token
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", usernameAndPassword.getId());
            claims.put("username", usernameAndPassword.getUsername());
            String token = JwtUtils.generateJwt(claims);

            LoginInfo loginInfo = new LoginInfo(usernameAndPassword.getId(), usernameAndPassword.getUsername(), usernameAndPassword.getName(), token);
            return loginInfo;
        }
        return null;
    }

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        PageHelper.startPage(page, pageSize);
//        List<Emp> rows = empMapper.list();
//        Page<Emp> p = (Page<Emp>) rows;
//        return new PageBean(p.getTotal(), p.getResult());
//
//    }

//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        long total = empMapper.selectTotal();
//        Integer start = (page - 1) * pageSize;
//        List<Emp> rows = empMapper.selectByPage(start, pageSize);
//        return new PageBean(total, rows);
//    }
}
